package cn.knightzz.util;

import cn.knightzz.entity.SysFile;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 阿里云 OSS 文档 https://help.aliyun.com/document_detail/32008.html?spm=a2c4g.11186623.6.922.5ca54350urQeRk
 * @author knightzz
 * @date 2021/5/1 17:32
 */
public class OSSUtils {
    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private static String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
    private static String accessKeyId = "LTAI4GCji92Mc3wK92y2Ej9K";
    private static String accessKeySecret = "5EKEdG8TGIPSphpeQloNBen66Sgo2P";
    private static String bucketName = "test-sdk98";
    private static String rootPath = "test/";

    // 创建OSS客户端
    private final static OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    public static void upload(String targetName, InputStream stream){

        // targetName 上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如 abc/efg/123.jpg。
        if(ossClient.doesBucketExist(bucketName)){
            // 判断存储空间是否存在
            PutObjectResult result = ossClient.putObject(bucketName, targetName, stream);
            System.out.println(result.getResponse());
        }else{
            System.out.println("bucket 不存在");
        }
    }

    public static SysFile uploadByGeneratorPath(String fileName,InputStream stream){
        SysFile sysFile = null;
        // targetName 上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如 abc/efg/123.jpg。
        if(ossClient.doesBucketExist(bucketName)){
            // 判断存储空间是否存在
            LocalDateTime localDate = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 获取当前的时间戳 作为文件名
            long epochMilli = localDate.toInstant(ZoneOffset.of("+8")).toEpochMilli();
            String dir = formatter.format(localDate);
            String targetName = rootPath + dir + "/"+ epochMilli + "-" + fileName;

            PutObjectResult object = ossClient.putObject(bucketName, targetName, stream);
            // 封装文件信息
            sysFile = new SysFile();
            sysFile.setFileName(epochMilli + "-" + fileName);
            sysFile.setFilePath(targetName);

        }else{
            System.out.println("bucket 不存在");
        }
        return sysFile;
    }

    public static String getPathByTime(String file){
        LocalDateTime localDate = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 获取当前的时间戳 作为文件名
        long fileName = localDate.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        String dir = formatter.format(localDate);
        return rootPath + dir+ "/"+ fileName + "-" + file;
    }

    public static String getFileInfo(String targetName){
        if(ossClient.doesBucketExist(bucketName)){
            // 判断bucket是否存在
            OSSObject object = ossClient.getObject(bucketName, targetName);
            System.out.println(object);
            return object.getKey();
        }else{
            System.out.println("bucket 不存在");
        }
        return null;
    }

    /**
     * 根据文件路径删除文件
     * @param targetName
     * @return
     */
    public static String deleteFile(String targetName){
        if(ossClient.doesBucketExist(bucketName)){
            // 判断bucket是否存在
            ossClient.deleteObject(bucketName, targetName);
        }else{
            System.out.println("bucket 不存在");
        }
        return null;
    }
}
